package com.xydtech.ecard.user.service.impl;

import com.xydtech.common.core.domain.entity.SysDictData;
import com.xydtech.common.core.text.Convert;
import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.user.domain.EcardApiAdeptPrdtype;
import com.xydtech.ecard.user.domain.EcardApiEcardRegistInfo;
import com.xydtech.ecard.user.domain.EcardApiHotPrd;
import com.xydtech.ecard.user.mapper.EcardApiEcardRegistInfoMapper;
import com.xydtech.ecard.user.service.IEcardApiEcardRegistInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 电子名片用户注册Service业务层处理
 *
 * @author Daniel
 * @date 2021-08-30
 */
@Service
public class EcardApiEcardRegistInfoServiceImpl implements IEcardApiEcardRegistInfoService {
    @Autowired
    private EcardApiEcardRegistInfoMapper ecardApiEcardRegistInfoMapper;

    /**
     * 根据registId查询用户注册信息
     * @param registId
     * @return
     */
    @Override
    public EcardApiEcardRegistInfo selectEcardApiEcardRegistInfoByRegistId(Long registId, String salt) {
        EcardApiEcardRegistInfo ecardRegistInfo = ecardApiEcardRegistInfoMapper.selectEcardApiEcardRegistInfoByRegistId(registId);
        if (ecardRegistInfo != null) {
            try {
                //数据解密（手机号、身份证号）
                if (!StringUtils.objIsNullOrEmpty(ecardRegistInfo.getPhoneno())) {
                    ecardRegistInfo.setPhoneno(AESEncryptionUtils.decrypt(ecardRegistInfo.getPhoneno(), salt));
                }
                if (!StringUtils.objIsNullOrEmpty(ecardRegistInfo.getCardNum())) {
                    ecardRegistInfo.setCardNum(AESEncryptionUtils.decrypt(ecardRegistInfo.getCardNum(), salt));
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return ecardRegistInfo;
    }

    /**
     * 根据openId获取注册用户信息
     * @param openId
     * @return
     */
    @Override
    public EcardApiEcardRegistInfo selectEcardApiEcardRegistInfoByOpenId(String openId, String salt) {
        EcardApiEcardRegistInfo ecardRegistInfo = ecardApiEcardRegistInfoMapper.selectEcardApiEcardRegistInfoByOpenId(openId);
        if (ecardRegistInfo != null) {
            if (ecardRegistInfo != null) {
                try {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(ecardRegistInfo.getPhoneno())) {
                        ecardRegistInfo.setPhoneno(AESEncryptionUtils.decrypt(ecardRegistInfo.getPhoneno(), salt));
                    }
                    if (!StringUtils.objIsNullOrEmpty(ecardRegistInfo.getCardNum())) {
                        ecardRegistInfo.setCardNum(AESEncryptionUtils.decrypt(ecardRegistInfo.getCardNum(), salt));
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return ecardRegistInfo;
    }

    /**
     * 根据员工编号获取注册用户信息
     * @param userCode
     * @return
     */
    @Override
    public EcardApiEcardRegistInfo selectEcardApiEcardRegistInfoByUserCode(String userCode, String salt) {
        EcardApiEcardRegistInfo ecardRegistInfo = ecardApiEcardRegistInfoMapper.selectEcardApiEcardRegistInfoByUserCode(userCode);
        if (ecardRegistInfo != null){
            try {
                //数据解密（手机号、身份证号）
                if (!StringUtils.objIsNullOrEmpty(ecardRegistInfo.getPhoneno())) {
                    ecardRegistInfo.setPhoneno(AESEncryptionUtils.decrypt(ecardRegistInfo.getPhoneno(), salt));
                }
                if (!StringUtils.objIsNullOrEmpty(ecardRegistInfo.getCardNum())) {
                    ecardRegistInfo.setCardNum(AESEncryptionUtils.decrypt(ecardRegistInfo.getCardNum(), salt));
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return ecardRegistInfo;
    }

    /**
     * 新增电子名片用户注册
     *
     * @param ecardApiEcardRegistInfo 电子名片用户注册
     * @return 结果
     */
    @Override
    public int insertEcardApiEcardRegistInfo(EcardApiEcardRegistInfo ecardApiEcardRegistInfo, String salt) {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardApiEcardRegistInfo.getPhoneno())) {
                ecardApiEcardRegistInfo.setPhoneno(AESEncryptionUtils.encrypt(ecardApiEcardRegistInfo.getPhoneno(), salt));
            }
            if (!StringUtils.objIsNullOrEmpty(ecardApiEcardRegistInfo.getCardNum())) {
                ecardApiEcardRegistInfo.setCardNum(AESEncryptionUtils.encrypt(ecardApiEcardRegistInfo.getCardNum(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ecardApiEcardRegistInfoMapper.insertEcardApiEcardRegistInfo(ecardApiEcardRegistInfo);
    }

    /**
     * 更新头像信息
     * @param userId
     * @param avatarFilePath
     * @return
     */
    @Override
    public void updateAvatar(String userId, String avatarFilePath) {
        //更新注册信息头像
        ecardApiEcardRegistInfoMapper.updateAvatar(userId, avatarFilePath);
        //注册系统信息头像
        ecardApiEcardRegistInfoMapper.updateSysAvatar(userId, avatarFilePath);
    }

    /**
     * 更新微信二维码
     * @param userId
     * @param wechatQrcodeFilePath
     * @return
     */
    @Override
    public int updateWechatQrcode(String userId, String wechatQrcodeFilePath) {
        return ecardApiEcardRegistInfoMapper.updateWechatQrcode(userId, wechatQrcodeFilePath);
    }

    /**
     * 电子名片-创建工作室-信息完善
     * @param ecardRegistInfo
     * @return
     */
    @Override
    public int updateUserInfo(EcardApiEcardRegistInfo ecardRegistInfo, String salt) {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardRegistInfo.getPhoneno())) {
                ecardRegistInfo.setPhoneno(AESEncryptionUtils.encrypt(ecardRegistInfo.getPhoneno(), salt));
            }
            if (!StringUtils.objIsNullOrEmpty(ecardRegistInfo.getCardNum())) {
                ecardRegistInfo.setCardNum(AESEncryptionUtils.encrypt(ecardRegistInfo.getCardNum(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        //更新用户注册信息为营销经理，然后将头像同步到基础信息表
        int saveCount = ecardApiEcardRegistInfoMapper.updateUserInfo(ecardRegistInfo);
        saveCount += ecardApiEcardRegistInfoMapper.syncAvatarPathToSys(ecardRegistInfo);
        return saveCount;
    }

    /**
     * 电子名片-创建工作室-保存擅长业务和优选产品
     * @param prdTypes
     * @param prdIds
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAdeptPrdTypeAndHotPrd(String currentUserId, String prdTypes, String prdIds, Long deptId) {
        //首先删除掉之前的产品配置信息
        ecardApiEcardRegistInfoMapper.delAdeptPrdTypeByUserId(currentUserId);
        ecardApiEcardRegistInfoMapper.delHotPrdByUserId(currentUserId);
        //然后再保存擅长领域和推荐业务
        ecardApiEcardRegistInfoMapper.saveAdeptPrdType(currentUserId, Convert.toStrArray(prdTypes), deptId);
        ecardApiEcardRegistInfoMapper.saveHotPrd(currentUserId, prdIds);
    }

    /**
     * 根据法人机构查询对应的产品类表
     * @param parentDeptId
     * @return
     */
    @Override
    public List<SysDictData> getPrdType(Long parentDeptId){
        return ecardApiEcardRegistInfoMapper.getPrdType(parentDeptId);
    }


    /**
     * 获取客户经理的擅长领域
     * @param userId
     * @return
     */
    @Override
    public List<EcardApiAdeptPrdtype> getAdeptPrdTypeList(String userId) {
        return ecardApiEcardRegistInfoMapper.getAdeptPrdTypeList(userId);
    }

    /**
     * 获取客户经理的热门产品
     * @param userId
     * @return
     */
    @Override
    public List<EcardApiHotPrd> getHotPrdList(String userId) {
        return ecardApiEcardRegistInfoMapper.getHotPrdList(userId);
    }

    /**
     * 电子名片-创建工作室-获取头像和微信二维码路径
     * @param userId
     * @return
     */
    @Override
    public Map<String, Object> getAvatarAndWechatQrcode(String userId) {
        List<Map<String, Object>> avatarAndWechatQrcodeList = ecardApiEcardRegistInfoMapper.getAvatarAndWechatQrcode(userId);
        return avatarAndWechatQrcodeList.size() > 0 ? avatarAndWechatQrcodeList.get(0) : new HashMap<>();
    }

    /**
     * 电子名片-注册信息-完善用户信息
     * @param userId
     * @param nickName
     * @param avatarUrl
     * @return
     */
    @Override
    public int completeUserInfo(String userId, String nickName, String avatarUrl) {
        return ecardApiEcardRegistInfoMapper.completeUserInfo(userId, nickName, avatarUrl);
    }
    /**
     * 客户经理用户注销时使用
     *
     * @author yjs
     * @date 2022/11/11 14:34
     */
    @Override
    public int logoffManagerUser(String registId) {
        return ecardApiEcardRegistInfoMapper.logoffManagerUser(registId);
    }

    /**
     * 更新注册表员工号
     * @param ecardRegistInfo
     * @param salt
     * @return
     */
    @Override
    public int updateUserCode(EcardApiEcardRegistInfo ecardRegistInfo, String salt) {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardRegistInfo.getPhoneno())) {
                ecardRegistInfo.setPhoneno(AESEncryptionUtils.encrypt(ecardRegistInfo.getPhoneno(), salt));
            }
            if (!StringUtils.objIsNullOrEmpty(ecardRegistInfo.getCardNum())) {
                ecardRegistInfo.setCardNum(AESEncryptionUtils.encrypt(ecardRegistInfo.getCardNum(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ecardApiEcardRegistInfoMapper.updateUserCode(ecardRegistInfo);
    }
}
